package com.example.daojia.frametest.Activity;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;

import com.alibaba.android.arouter.facade.annotation.Route;
import com.alibaba.fastjson.parser.deserializer.ObjectDeserializer;
import com.example.daojia.frametest.Common.rxjavaFunction;
import com.example.daojia.frametest.R;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;
import java.util.concurrent.TimeUnit;

import io.reactivex.Observable;
import io.reactivex.ObservableEmitter;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.ObservableSource;
import io.reactivex.Observer;
import io.reactivex.annotations.NonNull;
import io.reactivex.disposables.Disposable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;


@Route(path = "/app/rxjava")
public class rxjava extends AppCompatActivity {
    private static final String TAG = rxjava.class.getSimpleName() + "TAG";
    Integer i = 0;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_rx);
        rxJavaTest();
    }

    private void rxJavaTest() {
//        //Create()方法
//        Observable.create(new ObservableOnSubscribe<Integer>() {
//            @Override
//            public void subscribe(ObservableEmitter<Integer> e) throws Exception {
//                e.onNext(1);
//                e.onNext(2);
//                e.onNext(3);
//                e.onNext(4);
//                e.onNext(5);
//                e.onComplete();
//            }
//        }).subscribe(new Observer<Integer>() {
//            @Override
//            public void onSubscribe(@NonNull Disposable d) {
//                Log.w(TAG, "onSubscribe" + " onSubscribe= " + "onSubscribe");
//            }
//
//            @Override
//            public void onNext(@NonNull Integer integer) {
//                Log.w(TAG, "onSubscribe" + " onNext= " + integer);
//            }
//
//            @Override
//            public void onError(@NonNull Throwable e) {
//                Log.w(TAG, "onSubscribe" + " onError= " + e.getStackTrace().toString());
//            }
//
//            @Override
//            public void onComplete() {
//                Log.w(TAG, "onSubscribe" + " onComplete= " + "onComplete");
//            }
//        });

        Observer observer = new Observer<Integer>() {
            @Override
            public void onSubscribe(@NonNull Disposable d) {
                Log.w(TAG, "onSubscribe" + " onSubscribe= " + "onSubscribe");
            }

            @Override
            public void onNext(@NonNull Integer integer) {
                Log.w(TAG, "onSubscribe" + " onNext= " + integer);
            }

            @Override
            public void onError(@NonNull Throwable e) {
                Log.w(TAG, "onSubscribe" + " onError= " + e.getStackTrace().toString());
            }

            @Override
            public void onComplete() {
                Log.w(TAG, "onSubscribe" + " onComplete= " + "onComplete");
            }
        };

        Consumer consumer = new Consumer<Integer>() {
            @Override
            public void accept(Integer o) throws Exception {
                Log.w(TAG, "onSubscribe" + " = " + o);
            }
        };

//        //Just方法
//        Observable.just(1,2,3,4,5)
//                .subscribe(observer);
//        Observable.just(1,2,3,4,5)
//                .subscribe(consumer);
//        //FromArrary
//        Integer[] integer = {1,2,3,4,5,6};
//        Observable.fromArray(integer)
//                .subscribe(consumer);

//        Observable.fromCallable(new Callable<Integer>() {
//            @Override
//            public Integer call() throws Exception {
//
//                return 1;
//            }
//        }).subscribe(consumer);

//        List<Integer> list = new ArrayList<>();
//        list.add(0);
//        list.add(1);
//        list.add(2);
//        list.add(3);
//        Observable.fromIterable(list)
//                .subscribe(consumer);


//        Observable<Integer> observerable = Observable.defer(new Callable<Observable<Integer>>() {
//            @Override
//            public Observable<Integer> call() throws Exception {
//                return Observable.just(i);
//            }
//        });
//
//        observerable.subscribe(consumer);
//        i = 2;
//        observerable.subscribe(consumer);

//        Observable.timer(2, TimeUnit.SECONDS)
//                .subscribe(observer);

//        Observable.intervalRange(2, 5, 2, 3, TimeUnit.SECONDS)
//                .subscribe(new Observer<Long>() {
//                    @Override
//                    public void onSubscribe(@NonNull Disposable d) {
//                        Log.w(TAG, "onSubscribe" + " = " + "onSubscribe");
//                    }
//
//                    @Override
//                    public void onNext(@NonNull Long aLong) {
//                        Log.w(TAG, "onSubscribe" + " = " + aLong);
//                    }
//
//                    @Override
//                    public void onError(@NonNull Throwable e) {
//                        Log.w(TAG, "onSubscribe" + " = " + "onError");
//                    }
//
//                    @Override
//                    public void onComplete() {
//                        Log.w(TAG, "onSubscribe" + " = " + "onComplete");
//                    }
//                });
//        Observable.just(1,2,3,4,5)
//                .map(new Function<Integer, String>() {
//                    @Override
//                    public String apply(@NonNull Integer integer) throws Exception {
//                        return "" + integer;
//                    }
//                }).subscribe(new Consumer<String>() {
//            @Override
//            public void accept(String s) throws Exception {
//                Log.w(TAG, "onSubscribe" + " = " + s);
//            }
//        });

//        new rxjavaFunction().getValue(2)
//                .subscribe(new Consumer<Integer>() {
//                    @Override
//                    public void accept(Integer integer) throws Exception {
//                        Log.w(TAG, "onSubscribe" + " = " + integer);
//                    }
//                });
        class evenBean {
            private String tag;
            public evenBean(String tag) {
                this.tag = tag;
            }

            public String getTag() {
                return tag;
            }
        }

//        Observable.create(new ObservableOnSubscribe<evenBean>() {
//            @Override
//            public void subscribe(@NonNull ObservableEmitter<evenBean> e) throws Exception {
//                evenBean bean = new evenBean("out mine");
//                e.onNext(bean);
//            }
//        }).subscribe(new Consumer<evenBean>() {
//            @Override
//            public void accept(evenBean evenBean) throws Exception {
//                Log.w(TAG, "onSubscribe" + " = " + evenBean.getTag());
//            }
//        });

//        evenBean bean = new evenBean("out mine");
//        Observable.just(bean)
//                .subscribe(new Consumer<evenBean>() {
//                    @Override
//                    public void accept(evenBean evenBean) {
//                        Log.w(TAG, "onSubscribe" + " = " + evenBean.getTag());
//                    }
//                }, new Consumer<Throwable>() {
//                    @Override
//                    public void accept(Throwable throwable) throws Exception {
//
//                    }
//                });

        Observable.sequenceEqual(Observable.just(1,1,1), Observable.just(1,2,3))
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.w(TAG, "onSubscribe" + " = " + aBoolean);
                    }
                });
        Observable.empty()
                .isEmpty()
                .subscribe(new Consumer<Boolean>() {
                    @Override
                    public void accept(Boolean aBoolean) throws Exception {
                        Log.w(TAG, "onSubscribe" + " = " + aBoolean);
                    }
                });


    }
}
